﻿using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using StockholmsStad.Windows.UI.Modules.Ceremony;
using StockholmsStad.Windows.UI.Modules.Couple;
using StockholmsStad.Windows.UI.Modules.MainModule.Interfaces;
using StockholmsStad.Windows.UI.Modules.Maintenance;
using StockholmsStad.Windows.UI.Modules.Minister;
using StockholmsStad.Windows.UI.Modules.WatchList;
using RegionNames=StockholmsStad.Windows.UI.Infrastructure.RegionNames;
using ViewNames=StockholmsStad.Windows.UI.Modules.Couple.ViewNames;

namespace StockholmsStad.Windows.UI.Modules.MainModule.Controllers
{
    public class ViewController : IViewController
    {
        private readonly IUnityContainer container;
        private readonly IRegionManager regionManager;

        public ViewController(IUnityContainer container, IRegionManager regionManager)
        {
            this.container = container;
            this.regionManager = regionManager;
        }

        #region IViewController Members

        public void OnViewSelected(System.Windows.FrameworkElement frameworkElement)
        {
            RemoveCurrentView();

            switch (frameworkElement.Name)
            {
                case "Couple":
                    {
                        var coupleModule = container.Resolve<CoupleModule>();
                        coupleModule.LoadView();
                        break;
                    }
                case "Ceremony":
                    {
                        var ceremonyModule = container.Resolve<CeremonyModule>();
                        ceremonyModule.LoadView();
                        break;
                    }
                case "Minister":
                    {
                        var ministerModule = container.Resolve<MinisterModule>();
                        ministerModule.LoadView();
                        break;
                    }
                case "Maintenance":
                    {
                        var maintenanceModule = container.Resolve<MaintenanceModule>();
                        maintenanceModule.LoadView();
                        break;
                    }
                case "WatchList":
                    {
                        var watchListModule = container.Resolve<WatchListModule>();
                        watchListModule.LoadView();
                        break;
                    }
                default:
                    break;
            }
        }

        private void RemoveCurrentView()
        {
            if (regionManager.Regions.ContainsRegionWithName(RegionNames.ScheduleRegion))
            {
                var scheduleRegion = regionManager.Regions[RegionNames.ScheduleRegion];

                if (scheduleRegion != null)
                {
                    if (scheduleRegion.GetView(Maintenance.ViewNames.ScheduleView) != null)
                        scheduleRegion.Remove(scheduleRegion.GetView(Maintenance.ViewNames.ScheduleView));
                }
            }

            var contentRegion = regionManager.Regions[RegionNames.ContentRegion];

            if (contentRegion != null)
            {
                if (contentRegion.GetView(ViewNames.ListView) != null)
                {
                    contentRegion.Remove(contentRegion.GetView(ViewNames.ListView));
                    if (regionManager.Regions.ContainsRegionWithName("BookingRegion"))
                        regionManager.Regions.Remove("BookingRegion");
                }

                if (contentRegion.GetView(Minister.ViewNames.ListView) != null)
                    contentRegion.Remove(contentRegion.GetView(Minister.ViewNames.ListView));

                if (contentRegion.GetView(Ceremony.ViewNames.ListView) != null)
                    contentRegion.Remove(contentRegion.GetView(Ceremony.ViewNames.ListView));

                if (contentRegion.GetView(WatchList.ViewNames.ListView) != null)
                    contentRegion.Remove(contentRegion.GetView(WatchList.ViewNames.ListView));

                if (contentRegion.GetView(Maintenance.ViewNames.MaintenanceView) != null)
                    contentRegion.Remove(contentRegion.GetView(Maintenance.ViewNames.MaintenanceView));
            }

        }

        #endregion
    }
}
